home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GP_MSWTX.C < prev    next >
C/C++ Source or Header  |  1993-05-01  |  21KB  |  772 lines

  1. /* Copyright (C) 1993  Russell Lang
  2.  
  3. Permission to use, copy, modify, distribute, and sell this
  4. software and its documentation for any purpose is hereby granted
  5. without fee, provided that the above copyright notice and this
  6. permission notice appear in all copies of the software and related
  7. documentation.
  8. */
  9.  
  10. /* gp_mswtx.c */
  11. /*
  12.  * Microsoft Windows 3.n text window for Ghostscript.
  13.  * Original version by Russell Lang
  14.  */
  15.  
  16. #define STRICT
  17. #include "windows_.h"
  18. #include <windowsx.h>
  19. #if WINVER >= 0x030a
  20. #include <commdlg.h>
  21. #include <shellapi.h>
  22. #endif
  23. #include "ctype_.h"
  24. #include "memory_.h"
  25. #include "string_.h"    /* use only far items */
  26. #include <stdlib.h>
  27. #include "dos_.h"
  28.  
  29. #include "gp_mswin.h"
  30. #include "gp_mswtx.h"
  31.  
  32. /* sysmenu */
  33. #define M_COPY_CLIP 1
  34. /* font stuff */
  35. #define TEXTFONTSIZE 9
  36. #define TEXTFONTNAME "Terminal"
  37.  
  38. /* limits */
  39. POINT ScreenMinSize = {16,4};
  40. LRESULT CALLBACK _export WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  41.  
  42. char szNoMemory[] = "out of memory";
  43. LPSTR szTextClass = "gstext_class";
  44.  
  45. void
  46. TextMessage(void)
  47. {
  48.     MSG msg;
  49.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  50.         {
  51.         TranslateMessage(&msg);
  52.         DispatchMessage(&msg);
  53.         }
  54.     return;
  55. }
  56.  
  57.  
  58. void
  59. CreateTextClass(LPTW lptw)
  60. {
  61. WNDCLASS wndclass;
  62. static BOOL init = FALSE;
  63.     if (init)
  64.         return;
  65.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  66.     wndclass.lpfnWndProc = WndTextProc;
  67.     wndclass.cbClsExtra = 0;
  68.     wndclass.cbWndExtra = sizeof(void FAR *);
  69.     wndclass.hInstance = lptw->hInstance;
  70.     if (lptw->hIcon)
  71.         wndclass.hIcon = lptw->hIcon;
  72.     else
  73.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  74.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  75.     wndclass.hbrBackground = GetStockBrush(WHITE_BRUSH);
  76.     wndclass.lpszMenuName = NULL;
  77.     wndclass.lpszClassName = szTextClass;
  78.     RegisterClass(&wndclass);
  79.     init = TRUE;
  80. }
  81.  
  82.  
  83. /* make text window */
  84. int
  85. TextInit(LPTW lptw)
  86. {
  87.     HMENU sysmenu;
  88.     HGLOBAL hglobal;
  89.     
  90.     if (!lptw->hPrevInstance)
  91.         CreateTextClass(lptw);
  92.  
  93.     if (lptw->KeyBufSize == 0)
  94.         lptw->KeyBufSize = 256;
  95.  
  96.     if (lptw->ScreenSize.x < ScreenMinSize.x)
  97.         lptw->ScreenSize.x = ScreenMinSize.x;
  98.     if (lptw->ScreenSize.y < ScreenMinSize.y)
  99.         lptw->ScreenSize.y = ScreenMinSize.y;
  100.  
  101.     lptw->CursorPos.x = lptw->CursorPos.y = 0;
  102.     lptw->bFocus = FALSE;
  103.     lptw->bGetCh = FALSE;
  104.     lptw->CaretHeight = 0;
  105.     if (!lptw->nCmdShow)
  106.         lptw->nCmdShow = SW_SHOWNORMAL;
  107.  
  108.     hglobal = GlobalAlloc(GHND, lptw->ScreenSize.x * lptw->ScreenSize.y);
  109.     lptw->ScreenBuffer = (BYTE FAR *)GlobalLock(hglobal);
  110.     if (lptw->ScreenBuffer == (BYTE FAR *)NULL) {
  111.         MessageBox((HWND)NULL,szNoMemory,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  112.         return(1);
  113.     }
  114.     _fmemset(lptw->ScreenBuffer, ' ', lptw->ScreenSize.x * lptw->ScreenSize.y);
  115.     hglobal = GlobalAlloc(LHND, lptw->KeyBufSize);
  116.     lptw->KeyBuf = (BYTE FAR *)GlobalLock(hglobal);
  117.     if (lptw->KeyBuf == (BYTE FAR *)NULL) {
  118.         MessageBox((HWND)NULL,szNoMemory,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  119.         return(1);
  120.     }
  121.     lptw->KeyBufIn = lptw->KeyBufOut = lptw->KeyBuf;
  122.  
  123.     lptw->hWndText = CreateWindow(szTextClass, lptw->Title,
  124.           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  125.           CW_USEDEFAULT, CW_USEDEFAULT,
  126.           CW_USEDEFAULT, CW_USEDEFAULT,
  127.           NULL, NULL, lptw->hInstance, lptw);
  128.     if (lptw->hWndText == (HWND)NULL) {
  129.         MessageBox((HWND)NULL,"Couldn't open text window",(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  130.         return(1);
  131.     }
  132.     ShowWindow(lptw->hWndText, lptw->nCmdShow);
  133.     sysmenu = GetSystemMenu(lptw->hWndText,0);    /* get the sysmenu */
  134.     AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  135.     AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  136.  
  137.     return(0);
  138. }
  139.  
  140. /* close a text window */
  141. void
  142. TextClose(LPTW lptw)
  143. {
  144.     HGLOBAL hglobal;
  145.  
  146.     /* close window */
  147.     if (lptw->hWndText)
  148.         DestroyWindow(lptw->hWndText);
  149.     TextMessage();
  150.  
  151.     hglobal = (HGLOBAL)GlobalHandle( SELECTOROF(lptw->ScreenBuffer) );
  152.     if (hglobal) {
  153.         GlobalUnlock(hglobal);
  154.         GlobalFree(hglobal);
  155.     }
  156.     hglobal = (HGLOBAL)GlobalHandle( SELECTOROF(lptw->KeyBuf) );
  157.     if (hglobal) {
  158.         GlobalUnlock(hglobal);
  159.         GlobalFree(hglobal);
  160.     }
  161.     lptw->hWndText = (HWND)NULL;
  162. }
  163.     
  164.  
  165. /* Bring Cursor into text window */
  166. void
  167. TextToCursor(LPTW lptw)
  168. {
  169. int nXinc=0;
  170. int nYinc=0;
  171. int cxCursor;
  172. int cyCursor;
  173.     cyCursor = lptw->CursorPos.y * lptw->CharSize.y;
  174.     if ( (cyCursor + lptw->CharSize.y > lptw->ScrollPos.y + lptw->ClientSize.y) 
  175.       || (cyCursor < lptw->ScrollPos.y) ) {
  176.         nYinc = max(0, cyCursor + lptw->CharSize.y - lptw->ClientSize.y) - lptw->ScrollPos.y;
  177.         nYinc = min(nYinc, lptw->ScrollMax.y - lptw->ScrollPos.y);
  178.     }
  179.     cxCursor = lptw->CursorPos.x * lptw->CharSize.x;
  180.     if ( (cxCursor + lptw->CharSize.x > lptw->ScrollPos.x + lptw->ClientSize.x)
  181.       || (cxCursor < lptw->ScrollPos.x) ) {
  182.         nXinc = max(0, cxCursor + lptw->CharSize.x - lptw->ClientSize.x/2) - lptw->ScrollPos.x;
  183.         nXinc = min(nXinc, lptw->ScrollMax.x - lptw->ScrollPos.x);
  184.     }
  185.     if (nYinc || nXinc) {
  186.         lptw->ScrollPos.y += nYinc;
  187.         lptw->ScrollPos.x += nXinc;
  188.         ScrollWindow(lptw->hWndText,-nXinc,-nYinc,NULL,NULL);
  189.         SetScrollPos(lptw->hWndText,SB_VERT,lptw->ScrollPos.y,TRUE);
  190.         SetScrollPos(lptw->hWndText,SB_HORZ,lptw->ScrollPos.x,TRUE);
  191.         UpdateWindow(lptw->hWndText);
  192.     }
  193. }
  194.  
  195. void
  196. NewLine(LPTW lptw)
  197. {
  198.     lptw->CursorPos.x = 0;
  199.     lptw->CursorPos.y++;
  200.     if (lptw->CursorPos.y >= lptw->ScreenSize.y) {
  201.         int i =  lptw->ScreenSize.x * (lptw->ScreenSize.y - 1);
  202.         _fmemmove(lptw->ScreenBuffer, lptw->ScreenBuffer+lptw->ScreenSize.x, i);
  203.         _fmemset(lptw->ScreenBuffer + i, ' ', lptw->ScreenSize.x);
  204.         lptw->CursorPos.y--;
  205.         ScrollWindow(lptw->hWndText,0,-lptw->CharSize.y,NULL,NULL);
  206.         UpdateWindow(lptw->hWndText);
  207.     }
  208.     if (lptw->CursorFlag)
  209.         TextToCursor(lptw);
  210.     TextMessage();
  211. }
  212.  
  213. /* Update count characters in window at cursor position */
  214. /* Updates cursor position */
  215. void
  216. UpdateText(LPTW lptw, int count)
  217. {
  218. HDC hdc;
  219. int xpos, ypos;
  220.     xpos = lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x;
  221.     ypos = lptw->CursorPos.y*lptw->CharSize.y - lptw->ScrollPos.y;
  222.     hdc = GetDC(lptw->hWndText);
  223.     SelectFont(hdc, lptw->hfont);
  224.     TextOut(hdc,xpos,ypos,
  225.         (LPSTR)(lptw->ScreenBuffer + lptw->CursorPos.y*lptw->ScreenSize.x + 
  226.         lptw->CursorPos.x), count);
  227.     (void)ReleaseDC(lptw->hWndText,hdc);
  228.     lptw->CursorPos.x += count;
  229.     if (lptw->CursorPos.x >= lptw->ScreenSize.x)
  230.         NewLine(lptw);
  231. }
  232.  
  233. int
  234. TextPutCh(LPTW lptw, BYTE ch)
  235. {
  236. int pos;
  237.     switch(ch) {
  238.         case '\r':
  239.             lptw->CursorPos.x = 0;
  240.             if (lptw->CursorFlag)
  241.                 TextToCursor(lptw);
  242.             break;
  243.         case '\n':
  244.             NewLine(lptw);
  245.             break;
  246.         case 7:
  247.             MessageBeep(-1);
  248.             if (lptw->CursorFlag)
  249.                 TextToCursor(lptw);
  250.             break;
  251.         case '\t':
  252.             {
  253.             int n;
  254.                 for ( n = 8 - (lptw->CursorPos.x % 8); n>0; n-- )
  255.                     TextPutCh(lptw, ' ');
  256.             }
  257.             break;
  258.         case 0x08:
  259.         case 0x7f:
  260.             lptw->CursorPos.x--;
  261.             if (lptw->CursorPos.x < 0) {
  262.                 lptw->CursorPos.x = lptw->ScreenSize.x - 1;
  263.                 lptw->CursorPos.y--;
  264.             }
  265.             if (lptw->CursorPos.y < 0)
  266.                 lptw->CursorPos.y = 0;
  267.             break;
  268.         default:
  269.             pos = lptw->CursorPos.y*lptw->ScreenSize.x + lptw->CursorPos.x;
  270.             lptw->ScreenBuffer[pos] = ch;
  271.             UpdateText(lptw, 1);
  272.     }
  273.     return ch;
  274. }
  275.  
  276. void 
  277. TextWriteBuf(LPTW lptw, LPSTR str, int cnt)
  278. {
  279. BYTE FAR *p;
  280. int count, limit;
  281.     while (cnt>0) {
  282.     p = lptw->ScreenBuffer + lptw->CursorPos.y*lptw->ScreenSize.x + lptw->CursorPos.x;
  283.     limit = lptw->ScreenSize.x - lptw->CursorPos.x;
  284.     for (count=0; (count < limit) && (cnt>0) && (isprint(*str) || *str=='\t'); count++) {
  285.         if (*str=='\t') {
  286.         int n;
  287.         for ( n = 8 - ((lptw->CursorPos.x+count) % 8); (count < limit) & (n>0); n--, count++ ) {
  288.             *p++ = ' ';
  289.         }
  290.         str++;
  291.         count--;
  292.            }
  293.         else {
  294.         *p++ = *str++;
  295.         }
  296.         cnt--;
  297.     }
  298.     if (count>0) {
  299.         UpdateText(lptw, count);
  300.     }
  301.     if (cnt > 0) {
  302.         if (*str=='\n') {
  303.         NewLine(lptw);
  304.         str++;
  305.         cnt--;
  306.         }
  307.         else if (!isprint(*str) && *str!='\t') {
  308.         TextPutCh(lptw, *str++);
  309.         cnt--;
  310.         }
  311.     }
  312.     }
  313. }
  314.  
  315.  
  316. /* TRUE if key hit, FALSE if no key */
  317. int
  318. TextKBHit(LPTW lptw